﻿using System.Data.SQLite;

namespace RobosimAPI.Class
{
	public class Round
	{
		/*
		CREATE TABLE round (
        id               INTEGER,
        start_time       INTEGER,
        end_time         INTEGER,
        round_index      INTEGER,
        type             INTEGER,
        process          INTEGER,
        preload_map_mins INTEGER
        group_id         INTEGER,
        map_id           INTEGER,
        is_exam          INTEGER,
        is_limit         INTEGER,
        );
		*/
		public int Id;
        public long StartTime;
        public long EndTime;
        public int RoundIndex;
        public int Type;
        public int Process;
        public int PreloadMapMins;
        public int GroupId;
        public int MapId;
        public int IsExam;
        public int IsLimit;
        public Round() { }
        public Round(int round_id)
        {
			using (var conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
            {
                conn.Open();
                var cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM round WHERE id = @id";
                cmd.Parameters.AddWithValue("@id", round_id);
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Id = reader.GetInt32(0);
                    StartTime = reader.GetInt64(1);
                    EndTime = reader.GetInt64(2);
                    RoundIndex = reader.GetInt32(3);
                    Type = reader.GetInt32(4);
                    Process = reader.GetInt32(5);
                    PreloadMapMins = reader.GetInt32(6);
                    GroupId = reader.GetInt32(7);
                    MapId = reader.GetInt32(8);
                    IsExam = reader.GetInt32(9);
                    IsLimit = reader.GetInt32(10);
                }
                conn.Close();
                conn.Dispose();
            }

		}
        public static Round[] GetRoundWhereGroupId(int groupId)
        {
			using (var conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
            {
                conn.Open();
                var cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM round WHERE group_id = @groupId";
                cmd.Parameters.AddWithValue("@groupId", groupId);
                var reader = cmd.ExecuteReader();
                var rounds = new List<Round>();
                while (reader.Read())
                {
                    var round = new Round();
                    round.Id = reader.GetInt32(0);
                    round.StartTime = reader.GetInt64(1);
                    round.EndTime = reader.GetInt64(2);
                    round.RoundIndex = reader.GetInt32(3);
                    round.Type = reader.GetInt32(4);
                    round.Process = reader.GetInt32(5);
                    round.PreloadMapMins = reader.GetInt32(6);
                    round.GroupId = reader.GetInt32(7);
                    round.MapId = reader.GetInt32(8);
                    round.IsExam = reader.GetInt32(9);
                    round.IsLimit = reader.GetInt32(10);
                    rounds.Add(round);
                }
                conn.Close();
                conn.Dispose();
                return rounds.ToArray();
            }

		}
	}
}
